import {ref, watch, onMounted} from 'vue'

export default function use_recover(subject, user_answer) {
    const checkList = ref([])//选项卡
    const radio_value = ref(null)//单选框答案
    const input_value = ref(null)//输入框答案
    //恢复答案
    const recovery = () => {
        //如果传进了答案需要把答案填入到checkList
        if (user_answer.value) {
            switch (subject.value.subtype) {
                case "checkbox":
                    checkList.value = user_answer.value.split('')
                    break
                case "radio":
                    radio_value.value = user_answer.value
                    break
                case "input":
                    input_value.value = user_answer.value
                    break
            }
        } else {
            //否则清空选择项
            checkList.value = []
            radio_value.value = null
            input_value.value = null
        }
    }
    onMounted(recovery)
    watch(subject, recovery)
    // watch(subject, recovery, {immediate: true})
    return {
        checkList,
        radio_value,
        input_value
    }
}
